<template>
    <div class="wrap">
        <Tips :msg = "tips" v-show = "tipshow"/>
        <div class="details">
            <div class="title">博客详情</div>
            <div class="content">
                <div class="name">{{$route.query.name}}</div>
                <div class="info">
                    <div class="arouthName">作者：{{$route.query.arouthName}}</div>
                    <div class="addTime">发布时间：{{$route.query.addTime}}</div>
                </div>
                <div id="content">{{$route.query.content}}</div>
            </div>
            <div class="comment">
                <div class="title">评论区 <span class="add" @click="addComment">添加评论</span></div>
                <div class="list">
                    <div class="plq">
                        <div class="pl" v-for="item in commentList" :key="item._id">
                            <img src="@/assets/photo.png" alt="">
                            <div class="data">
                                <div class="userinfo">
                                    <span class="">{{item.username}}</span>
                                    <span class="time">{{item.addTime}}</span>
                                </div>
                                <div class="neirong">
                                    {{item.content}}
                                </div>
                            </div>
                            <div class="del" v-show="username === item.username" @click="delCommentRequest(item)">
                                删除此评论
                            </div>
                        </div>
                    </div>
                    
                </div>
            </div>    
        </div>
        <AddComment v-show="isAddComment" :show="isAddComment"/>
        <HomeFooter/>
    </div>
</template>

<script>
    import {mapState} from 'vuex'
    import Tips from '@/components/Tips'
    import AddComment from '@/components/addComment'
    import HomeFooter from '@/components/HomeFooter'
    export default {
        name:"Details-Component",
        components:{HomeFooter,AddComment,Tips},
        data(){
            return {
                arouthName:"",
                PostsInfo:"",
                isAddComment:false,
                comment:"",
                tips:"",
                tipshow:false,
                username: "",
                
            }
        },
        methods: {
            // 打开弹出层
            addComment(){
                const user = sessionStorage.getItem('user')
                if(user){
                    this.username = JSON.parse(user).username
                    this.isAddComment = true
                }else{
                    this.tips = '登陆后才能添加评论！'
                    this.tipshow = true
                    setTimeout(()=>{
                        this.tipshow = false
                        this.tips = ''
                    },1000)
                }
                
            },

            // 添加评论请求
            sendAddCommentRequest(){
                const _id = this.$route.query._id
                this.$store.dispatch('Details/sendAddCommentRequest',{_id,username:this.username,comment:this.comment})
            },

            // 获取该数据的评论
            getCommentList(){
                const _id = this.$route.query._id
                this.$store.dispatch('Details/getCommentList',_id)
            },

            //删除评论请求
            delCommentRequest(item){
                const _id = this.$route.query._id
                this.$store.dispatch('Details/delCommentList',{_id,comment_id:item._id})
                this.tips = '删除成功'
                this.tipshow = true
                setTimeout(()=>{
                    this.tips = ''
                    this.tipshow = false
                },1000)
                // 更新
                setTimeout(()=>{
                    this.getCommentList()
                },50)
            }


        },
        computed:{
            ...mapState('Details',['commentList'])
        },
        created() {
            this.getCommentList()
            const user = sessionStorage.getItem('user')
            if(user){
                this.username = JSON.parse(user).username
            }
        },
        mounted() {
           
            // 关闭弹出层
            this.$bus.$on('close',isAddComment => {
                this.isAddComment = isAddComment
            })

            // 获取值
            this.$bus.$on('getComment',comment=>{
                this.comment = comment
                // 发请求添加评论
                this.sendAddCommentRequest()
                // 更新
                this.getCommentList()

            })
        },

        beforeDestroy() {
            this.$bus.$off('close')
            this.$bus.$off('getComment')
        },
        
    }
</script>

<style scoped lang="less">
    .wrap{
        width: 100%;
        background: #F0F2F5;

        .details{
            width: 80%;
            margin:0 auto;
            
            padding-bottom: 2vw;

            .title{
                height: 3.3vw;
                border-bottom: 0.1vw solid rgb(233, 228, 228);
                line-height: 3.3vw;
                padding-left: 1.3vw;
                font-size: 1.2vw;
                background: #fff;
            }
        }

        .content{
            color: #333;
            background: #fff;
            padding: 2vw 0;
            .name{
                text-align: center;
                margin:1.3vw 0;
                font-size: 2.5vw;
                font-weight: 1000;
                
            }

            .info{
                display: flex;
                justify-content: center;
                color: rgb(112, 112, 112);
                font-size: 1.3vw;

                .addTime{
                    margin-left: 1.3vw;
                }
            }

            #content{
                text-indent: 2em;
                padding: 0.7vw 2.6vw;
                line-height: 3.5vw;
                margin-top: 2vw;
                font-size: 1.3vw;
            }
        }

        .comment{
            margin: 2vw 0;

            .add{
                text-align: right;
                display: inline-block;
                width: 90%;
                color: red;
                cursor: pointer;
                &:hover{
                    text-decoration: underline;
                }
            }

            

            img{
                width: 4.6vw;
                height: 4.6vw;
                vertical-align: middle;
            }

            .plq{
                background: #fff;
                padding: 2vw 2vw;

                .data{
                    width: 100%;
                }

                .pl{
                    padding: 2vw 0;
                    background: #F0F2F5;
                    display: flex;
                    align-content: center;
                    margin-bottom: 2vw;
                    padding-left: 2vw;
                    position: relative;
                }

                .del{
                    position: absolute;
                    width: 7vw;
                    background: #ccc;
                    height: 2.5vw;
                    text-align: center;
                    line-height: 2.5vw;
                    top: 50%;
                    transform: translateY(-50%);
                    right: 1.3vw;
                    background: #DB0F0F;
                    color: #fff;
                    cursor: pointer;
                    &:hover{
                        background: #a10909;
                    }
                }
                .userinfo{
                    display: flex;
                    align-items: center;
                    margin: 1vw 0;

                    
                    span{
                        margin-left: 2vw;
                        color: rgb(94, 94, 94);
                        font-size: 1.2vw;
                        font-weight: 1000;
                        
                    }                     
                }

                .neirong{
                    
                    padding:0 2vw;
                    padding-right: 10vw;
                    font-size: 1.2vw;
                    line-height: 2.6vw;
                    
                }
            }
        }
    }
</style>